<?php
/* explain:
 * Project:     
 * File:        InfoClass.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2008  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class Attributes extends Common
{
	public $conHandle;
	public $errorInfo;
	
	private $iUserID;
	
	private $objLog;
	
	function Attributes($strLanguage, $userid)
	{
		parent::Common();
		
		$this->conHandle 	= $this->getConnection($strLanguage);
		$this->objLog 		= new Logs($userid);
		$this->iUserID 		= $userid;
	}

	/* get news class via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getFromID($arrPopedom = array(), $languageID, $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM attributes WHERE id = $id";
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* get news class via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getByName($arrPopedom = array(), $languageID, $name)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM attributes WHERE languageID = $languageID AND name = '$name'";
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}
	
	/* 
	 *
	 * @param  string 	name		news class name 
	 * @param  string 	parentid	news parent ID  
	 * @param  string  	isLeaf    	is leaf node
 	 * @return int    	true: insert ID
	 */
    public function add($arrPopedom = array(), $name, $parentid, $sorts = -1, $isLeaf = 'no', $summary = '')
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO attributes SET name = '$name', parentid='$parentid', sorts = '$sorts', isLeaf = '$isLeaf', summary='$summary'";
		
		$rs  = mysql_query($sql, $this->conHandle) or die ($sql . mysql_error($this->conHandle));
		$intDataID = mysql_insert_id($this->conHandle);
		if($rs)
			return $intDataID;
		else
			return false;
	}

	/* edit
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function edit($arrPopedom = array(), $id, $name, $parentid, $sorts = -1, $isLeaf, $summary)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "id=$id");
			
		$sql = "UPDATE attributes SET name='$name', parentid='$parentid', sorts = '$sorts', isLeaf = '$isLeaf', summary='$summary' WHERE id = $id";
		$rs	 = mysql_query($sql, $this->conHandle);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
		
		$sql = "DELETE FROM attributes WHERE id = $id";
		$rs	 = mysql_query($sql, $this->conHandle);
		if($rs)
			return true;
		else
			return false;
	}

 
	
	/* 
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function isLeaf($arrPopedom = array(), $id)
	{
		$sql = "SELECT * FROM attributes WHERE isLeaf='YES' AND id = $id";
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs && mysql_num_rows($rs) > 0)
			return true;
		else
			return false;
	}
	
	/* 
	 *
	 * @param  string condition
	 * @return mixed  false
	 */
	public function listWeb($condition='')
	{
		$arrBack = array();
		$sql = "SELECT * FROM attributes".$condition;
        $rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}	
	
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();
		$sql = 'SELECT count(id) FROM attributes'.$where;

		$rs  = mysql_query($sql, $this->conHandle);
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = 'SELECT * FROM attributes'.$where;
		else{
			$sql = 'SELECT * FROM attributes'.$where." LIMIT $this->firstcount, $discount";
		}

		$rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* list all custom condition data
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listFromCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$arrBack = array();
		$sql = "SELECT * FROM attributes".$condition;
        $rs  = mysql_query($sql, $this->conHandle) or die($sql . mysql_error($this->conHandle));
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}
	
	public function getCount()
	{
		$sql = "SELECT count(id) FROM attributes";
		
		$rs = mysql_query($sql, $this->conHandle) or die(mysql_error());
		if($rs){
			$arrCache = mysql_fetch_array($rs);
			return $arrCache[0];
		}
		return 0;
	}
	
	public function getDir($arrOperate, $parentid)
	{
		$arrParentid 	= array();
		$arrTmpStack 	= array();
		$arrLevelStack 	= array();
		$arrClassid		= array();
		
		$i = 0;
		array_push($arrTmpStack, $parentid);
		
		while(sizeof($arrTmpStack) > 0){
			$strtmp = array_pop($arrTmpStack);
			$i = array_pop($arrLevelStack);
			if($strtmp > 0){
				$arrback = $this->getFromID($arrOperate, $strtmp);
				if(is_array($arrback)){
					$tmp = '';
					for($j = 0; $j < $i; $j++){
						$tmp.='&nbsp;&nbsp;&nbsp;&nbsp;';
					}
					$arrClassid[$arrback['id']] = $tmp.$arrback['name'];
				}
			}
		
			$mixBack = $this->listWeb(" WHERE parentid='$strtmp'");
			if(is_array($mixBack)){
				$i++;
				foreach($mixBack as $value){
					array_push($arrTmpStack,$value['id']);
					array_push($arrLevelStack,$i);
				}
			}
		}
		
		return $arrClassid;
	}
	
	public function arrGetDirIDList($parentid)
	{
		$arrParentid 	= array();
		$arrTmpStack 	= array();
		$arrLevelStack 	= array();
		$arrClassid		= array();
		
		$i = 0;
		array_push($arrTmpStack, $parentid);
		
		while(sizeof($arrTmpStack) > 0){
			$strtmp = array_pop($arrTmpStack);
			$i 		= array_pop($arrLevelStack);
			if($strtmp > 0){
				$sql = "SELECT isLeaf FROM attributes WHERE id = $strtmp";
				
				$rs  = mysql_query($sql, $this->conHandle) or die (mysql_error($this->conHandle));
				if($rs){
					$arrback = mysql_fetch_array($rs);
					
					if($arrback['isLeaf'] == 'yes')
						array_push($arrClassid, $strtmp);
				}
			}
		
			$sql = "SELECT id FROM attributes WHERE parentid=$strtmp";
			
	        $rs  = mysql_query($sql, $this->conHandle) or die (mysql_error($this->conHandle));
			if($rs){
				$mixBack = array();
				
				while($arrCache = mysql_fetch_array($rs)){
					$mixBack[] = $arrCache;
				}
				
				$i++;
				foreach($mixBack as $value){
					array_push($arrTmpStack,$value['id']);
					array_push($arrLevelStack,$i);
				}
			}
		}
		
		return $arrClassid;
	}
}
?>
